<?php

namespace api\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use api\models\User;

/**
 * UserSearch represents the model behind the search form about `api\models\User`.
 */
class UserSearch extends User
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'created_at', 'updated_at', 'gender', 'age'], 'integer'],
            [
                [
                    'auth_key',
                    'password_hash',
                    'password_reset_token',
                    'avatar',
                    'signature',
                    'description',
                    'real_name',
                    'phone',
                    'access_token',
                    'wx_openid',
                    'wx_avatar',
                    'wx_name',
                    'wx_session_key',
                    'email',
                    'username'
                ],
                'safe'
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = User::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id'         => $this->id,
            'status'     => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'gender'     => $this->gender,
            'age'        => $this->age,
        ]);

        $query->andFilterWhere(['like', 'auth_key', $this->auth_key])
              ->andFilterWhere(['like', 'password_hash', $this->password_hash])
              ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
              ->andFilterWhere(['like', 'avatar', $this->avatar])
              ->andFilterWhere(['like', 'signature', $this->signature])
              ->andFilterWhere(['like', 'description', $this->description])
              ->andFilterWhere(['like', 'real_name', $this->real_name])
              ->andFilterWhere(['like', 'phone', $this->phone])
              ->andFilterWhere(['like', 'access_token', $this->access_token])
              ->andFilterWhere(['like', 'wx_openid', $this->wx_openid])
              ->andFilterWhere(['like', 'wx_avatar', $this->wx_avatar])
              ->andFilterWhere(['like', 'wx_name', $this->wx_name])
              ->andFilterWhere(['like', 'wx_session_key', $this->wx_session_key])
              ->andFilterWhere(['like', 'email', $this->email])
              ->andFilterWhere(['like', 'username', $this->username]);

        return $dataProvider;
    }
}
